﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KnowledgeCommunity.Model;
using KnowledgeCommunity.Service;
using KnowledgeCommunity.Web.Models;
using KnowledgeCommunity.Web.DbHelper;
using System.IO;

namespace KnowledgeCommunity.Web.Controllers
{
    public class AnswerController : Controller
    {
        AnswerHelper myAnswerHelper = new AnswerHelper();
        //
        // GET: /Answer/
        [Authorize]
        public ActionResult Index()
        {
            var Answer = myAnswerHelper.FindAll();
            ViewData["AnswerList"] = Answer;
            return View();
        }


        [Authorize(Roles = "admin,editor")]
        public ActionResult List()
        {
            KnowledgeCommunity.Web.Models.Page Model = new Page();
            Model.CurrentPageIndex = 1;  //当前页 默认第一页

            int SkinCount = (Model.CurrentPageIndex - 1) * Model.PageSize;

            //测试用例。实际用自己的逻辑获取数据
            List<Answer> datesource = GetDataSource();//要分页的数据源
            Model.AnswerList = datesource.Where(r => r.Id >= 0).Skip(SkinCount).Take(Model.PageSize).ToList();
            //测试用例。实际用自己的逻辑获取数据

            //是否开启Ajax分页
            Model.IsAjax = true;
            Model.TotalCount = datesource.Count;//获取记录总数

            //获取分页数据
            Model.AnswerList = datesource.Where(r => r.Id >= 0).Skip(SkinCount).Take(Model.PageSize).ToList();
            return View(Model);

        }

        private List<Answer> GetDataSource()
        {
            var Answer = myAnswerHelper.FindAll();
            List<Answer> model = new List<Answer>();
            foreach (var item in Answer)
            {
                model.Add(item);
            }
            return model;
        }

        [Authorize(Roles = "admin,editor")]
        public ActionResult PageIndex(Models.Page Model)
        {

            //分页数据源。自己写逻辑获取。

            int SkinCount = (Model.CurrentPageIndex - 1) * Model.PageSize;

            //测试用例。实际用自己的逻辑获取数据
            List<Answer> datesource = GetDataSource();//要分页的数据源
            Model.AnswerList = datesource.Where(r => r.Id >= 0).Skip(SkinCount).Take(Model.PageSize).ToList();
            //测试用例。实际用自己的逻辑获取数据

            Model.TotalCount = datesource.Count;//获取记录总数
            if (Request.IsAjaxRequest())  //是AJAX请求的话返回AJAX分页数据
            {
                Model.IsAjax = true;
                return View(Model);
            }
            else
            {
                Model.IsAjax = false;
                return this.PartialView("List", Model);
            }
            //return View(Model);
        }

        [Authorize]
        public ActionResult Details(int id)
        {
            var _Answer = myAnswerHelper.FindById(id);
            return View(_Answer);
        }

        [Authorize]
        public ActionResult Create()
        {
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult Create(Answer model)
        {
            bool n = myAnswerHelper.Create(model);
            if (n)
                return List();
            else
            {
                return RedirectToAction("Error", "Notice", new { type = 1 });
            }
        }

        [Authorize]
        public ActionResult Edit(int id)
        {
            var _Answer = myAnswerHelper.FindById(id);
            return View(_Answer);
        }

        [Authorize]
        [HttpPost]
        public ActionResult Edit(Answer model)
        {
            DoUpdateModel(model);
            return RedirectToAction("Details", new { id = model.Id });
        }

        [Authorize]
        public ActionResult Delete(int id)
        {
            var _Answer = myAnswerHelper.FindById(id);
            return View(_Answer);
        }

        [Authorize]
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            bool n = myAnswerHelper.Delete(id);
            if (n)
                return List();
            else
                return RedirectToAction("Error", "Notice", new { type = 2 });
        }

        CKCDB mydb = new CKCDB();

        private void DoUpdateModel(Answer model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var app = mydb.Answers.Find(model.Id);
                    UpdateModel(app);
                    mydb.SaveChanges();
                }
                catch (Exception ex)
                {
                    string message = "Create in table<Answers>" + ex.ToString();
                    WriteLog.WriteSysLog(message);
                }
            }
        }

    }
}
